import Vue from 'vue';
import VueRouter from 'vue-router';
import { getItem } from '@/utils/storage';

// import Login from '@/components/login/login.vue';
// import Home from '@/components/home/home.vue';
// import Welcome from '@/components/welcome/welcome.vue';
// import User from '@/components/user/user.vue';
// import Rights from '@/components/power/rights.vue';
// import Roles from '@/components/power/roles.vue';
// import Category from '@/components/goods/category.vue';
// import Params from '@/components/goods/params.vue';
// import List from '@/components/goods/list.vue';
// import Add from '@/components/goods/add.vue';
// import Order from '@/components/order/order.vue';
// import Report from '@/components/report/report.vue';

Vue.use(VueRouter);

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('@/components/login/login.vue') },
  {
    path: '/home',
    component: () => import('@/components/home/home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('@/components/welcome/welcome.vue')
      },
      {
        path: '/users',
        component: () => import('@/components/user/user.vue')
      },
      {
        path: '/rights',
        component: () => import('@/components/power/rights.vue')
      },
      {
        path: '/roles',
        component: () => import('@/components/power/roles.vue')
      },
      {
        path: '/categories',
        component: () => import('@/components/goods/category.vue')
      },
      {
        path: '/params',
        component: () => import('@/components/goods/params.vue')
      },
      {
        path: '/goods',
        component: () => import('@/components/goods/list.vue')
      },
      {
        path: '/add',

        component: () => import('@/components/goods/add.vue')
      },
      {
        path: '/orders',
        component: () => import('@/components/order/order.vue')
      },
      {
        path: '/reports',
        component: () => import('@/components/report/report.vue')
      }
    ]
  }
];

const router = new VueRouter({
  routes
});

/* 添加路由守卫 */
router.beforeEach((to, from, next) => {
  /* 如果访问的是登录页面,直接放行 */
  if (to.path === '/login') return next();
  const token = getItem('token');
  /* 如果访问的页面没有token,就强制跳转到登录页 */
  if (!token) return next('/login');
  /* 其他情况放行 */
  next();
});

export default router;
